// import { fetchGoodsList } from '../../../services/good/fetchGoodsList';
import Toast from 'tdesign-miniprogram/toast/index';
import requestApi from "../../../../utils/request"

const initFilters = {
  overall: 1,
  sorts: '',
  layout: 0
};

Page({
  data: {
    goodsList: [],
    layout: 0,
    sorts: '',
    overall: 1,
    show: false,
    minVal: '',
    maxVal: '',
    filter: initFilters,
    hasLoaded: false,
    loadMoreStatus: 0,
    loading: true,
    //分类列表id
    classId: '',
    //搜索值
    searchValue: ''
  },

  page: 1,
  per_page: 30,
  total: 0,

  handleFilterChange(e) {
    const { layout, overall, sorts } = e.detail;
    this.page = 1;
    this.setData({
      layout,
      sorts,
      overall,
      loadMoreStatus: 0,
    });
    this.init(true);
  },

  generalQueryData(reset = false) {
    const { filter, keywords, minVal, maxVal } = this.data;
    const { page, per_page } = this;
    const { sorts, overall } = filter;
    const params = {
      sort: 0, // 0 综合，1 价格
      page: 1,
      per_page: 30,
      keyword: keywords,
    };

    if (sorts) {
      params.sort = 1;
      params.sortType = sorts === 'desc' ? 1 : 0;
    }

    if (overall) {
      params.sort = 0;
    } else {
      params.sort = 1;
    }
    params.minPrice = minVal ? minVal * 100 : 0;
    params.maxPrice = maxVal ? maxVal * 100 : undefined;
    if (reset) return params;
    return {
      ...params,
      page: page + 1,
      per_page,
    };
  },

  async init(reset = true, classId, searchValue) {

    const { loadMoreStatus, goodsList = [], overall, sorts } = this.data;
    const params = this.generalQueryData(reset);
    params.category_id = classId ? classId : this.classId
    params.keywords = searchValue ? searchValue : this.searchValue
    if (overall === 1) {
      params.store = 'weigh'
      params.order = 'desc'
    }
    if (overall === 0 && !sorts) {
      params.store = 'show_sales'
      params.order = 'desc'
    }
    if (overall === 0 && sorts) {
      params.store = 'price'
      params.order = sorts
    }
    if (loadMoreStatus !== 0) return;
    this.setData({
      loadMoreStatus: 1,
      loading: true,
    });
    try {
      requestApi.post('shopro/goods/lists', params).then(res => {
        const code = 'Success';
        const spuList = res.data;
        if (code.toUpperCase() === 'SUCCESS') {
          const { data, total = 0 } = spuList;
          if (total === 0 && reset) {
            this.total = total;
            this.setData({
              emptyInfo: {
                tip: '抱歉，未找到相关商品',
              },
              hasLoaded: true,
              loadMoreStatus: 0,
              loading: false,
              goodsList: [],
            });
            return;
          }

          const _goodsList = reset ? data : goodsList.concat(data);
          const _loadMoreStatus = _goodsList.length === total ? 2 : 0;
          this.page = params.page || 1;
          this.total = total;
          this.setData({
            goodsList: _goodsList,
            loadMoreStatus: _loadMoreStatus,
          });
        } else {
          this.setData({
            loading: false,
          });
          wx.showToast({
            title: '查询失败，请稍候重试',
          });
        }
      })

    } catch (error) {
      this.setData({
        loading: false,
      });
    }
    this.setData({
      hasLoaded: true,
      loading: false,
    });
  },

  onLoad(option) {
    this.classId = option.classId ? option.classId : ''
    this.searchValue = option.searchValue ? option.searchValue : ''
    this.init(true, option.classId, this.searchValue);
  },

  onReachBottom() {
    const { goodsList } = this.data;
    const { total = 0 } = this;
    if (goodsList.length === total) {
      this.setData({
        loadMoreStatus: 2,
      });
      return;
    }
    this.init(false);
  },

  handleAddCart() {
    Toast({
      context: this,
      selector: '#t-toast',
      message: '点击加购',
    });
  },

  tagClickHandle() {
    Toast({
      context: this,
      selector: '#t-toast',
      message: '点击标签',
    });
  },

  gotoGoodsDetail(e) {
    const { id } = e.detail.goods;
    wx.navigateTo({
      url: `/service/shoppingMall/Details/details/index?spuId=${id}`,
    });
  },

  showFilterPopup() {
    this.setData({
      show: true,
    });
  },

  showFilterPopupClose() {
    this.setData({
      show: false,
    });
  },

  onMinValAction(e) {
    const { value } = e.detail;
    this.setData({ minVal: value });
  },

  onMaxValAction(e) {
    const { value } = e.detail;
    this.setData({ maxVal: value });
  },

  reset() {
    this.setData({ minVal: '', maxVal: '' });
  },

  confirm() {
    const { minVal, maxVal } = this.data;
    let message = '';
    if (minVal && !maxVal) {
      message = `价格最小是${minVal}`;
    } else if (!minVal && maxVal) {
      message = `价格范围是0-${minVal}`;
    } else if (minVal && maxVal && minVal <= maxVal) {
      message = `价格范围${minVal}-${this.data.maxVal}`;
    } else {
      message = '请输入正确范围';
    }
    if (message) {
      Toast({
        context: this,
        selector: '#t-toast',
        message,
      });
    }
    this.page = 1;
    this.setData(
      {
        show: false,
        minVal: '',
        goodsList: [],
        loadMoreStatus: 0,
        maxVal: '',
      },
      () => {
        this.init();
      },
    );
  },
});
